package tp.pr2;
import java.util.List;
import java.util.ArrayList; 


public class ItemContainer{
	
private List<Item> itemList = new ArrayList<Item>();  
  

 public ItemContainer()
 {
	itemList.clear();
 } 
 
 public int numberOfItems(){
 return itemList.size();
 }
 
 public boolean addItem(Item id)
 {
	if(itemList.size()==0)
		{
		itemList.add(id);
		return true;
		}
	else
	{
	  if(searchItem(id.getId())==null)
	   {
		int i=0;
		while(i<itemList.size() && itemList.get(i).getId().compareToIgnoreCase(id.getId())<0)
		{
		 i++;	
		}
		itemList.add(i,id);
		return true;
	   }
	 else return false;
	}
 }
 
 private Integer searchItem(String id)
 {
	 Integer index=null;
	 for (int i = 0; i < itemList.size(); i++)
		{
		 if(itemList.get(i).getId().compareToIgnoreCase(id)==0)index=i;
		}
	return index; 
 }
 
 public Item getItem(java.lang.String id)
 {
   if(searchItem(id)==null)return null;
   return itemList.get(searchItem(id));		
 }

 public Item pickItem(java.lang.String id)
 {
	Integer index=searchItem(id);
	if(index==null)return null;
	Item item=itemList.get(index);
	itemList.remove(itemList.get(index));
	return item;
 }
 
 public java.lang.String toString()
 {
	 StringBuilder sb=new StringBuilder();
	 if (itemList.size()==0) return null;
	 for(int i = 0 ; i < itemList.size(); i++) {
			
			sb.append("   "+itemList.get(i).getId());
			if (i<itemList.size()-1)
			sb.append('\n');			
		}		
		return sb.toString();
 }

}